package controllers

import (
	"gdshop-font-api-go/app/request/CartReq"
	"gdshop-font-api-go/app/service/CartService"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools/token"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

type CartController struct {
	*BaseController
}

var memberId uint = 0

func (c *CartController) Init(r *ghttp.Request) *response.JsonResponse {
	memberId = token.GetLoginMemberId(r)
	if memberId < 1 {
		if r.URL.Path != "/cart/count" && r.URL.Path != "/cart/my" {
			return response.FailByRequestMessage(r, "获取会员ID失败")
		}
	}
	return response.SuccessByRequest(r)
}

// 我的购物车
func (c *CartController) My(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.My
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	if parames.MemberId < 1 {
		// 找不到登陆信息，不过不影响使用
		return response.JsonByAll(r, 10004, "成功", g.Map{
			"not_login": 1,
		})
	}
	return CartService.MyByGroup(r.GetCtx(), parames)
}

// 删除商品
func (c *CartController) Del(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.Del
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.Del(r.GetCtx(), parames)
}

// 保存购物车，自动处理新加入还是增加数量
func (c *CartController) Save(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.Save
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.Save(r.GetCtx(), parames)
}

// 保存勾选
func (c *CartController) SaveSelected(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.SaveSelected
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.SaveSelected(r.GetCtx(), parames)
}

// 全部勾选变动
func (c *CartController) SelectedAll(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.SelectedAll
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.SelectedAll(r.GetCtx(), parames)
}

// 获取购物车数量
func (c *CartController) Count(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.Count
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	// 如果 MemberId 为 0 则返回 0
	if parames.MemberId < 1 {
		// 找不到登陆信息，不过不影响使用
		return response.JsonByAll(r, 10004, "成功", 0)
	}
	return CartService.Count(r.GetCtx(), parames)
}

func (c *CartController) ChangePromotion(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.ChangePromotion
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.ChangePromotion(r.GetCtx(), parames)
}

func (c *CartController) Change(r *ghttp.Request) *response.JsonResponse {
	var parames *CartReq.Save
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return CartService.Change(r.GetCtx(), parames)
}
